﻿using Common.Extensions.OrderBy;
using DomainModel;
using IDataAccess;
using IManager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace Manager
{
    public class ManagerLinkes : IManagerLinks
    {
        /// <summary>
        /// 内容信息数据访问接口
        /// </summary>
        public IDataAccessLinks IDALinks { get; set; }


        /// <summary>
        /// 通过Id获取
        /// </summary>
        /// <param name="cid"></param>
        /// <param name="tid"></param>
        /// <returns>LinksInfo</returns>
        public LinksInfo GetLink(int cid, string tid)
        {
            Expression<Func<LinksInfo, bool>> where = e => e.CompanyId == cid && e.Types == tid;
            return IDALinks.Find(where);
        }

        public LinksInfo GetLink(int linkId)
        {
            Expression<Func<LinksInfo, bool>> where = e => e.Id == linkId;
            return IDALinks.Find(where);
        }

        public bool AddLink(LinksInfo link)
        {
            var res = IDALinks.Create(link);
            IDALinks.UnitOfWork.Commit();
            if (res != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool DeleteLink(IList<int> linkIdList)
        {
            try
            {
                foreach (var item in linkIdList)
                {
                    IDALinks.Delete(item);
                }

                IDALinks.UnitOfWork.Commit();
                return true;
            }
            catch (Exception)
            {
                return false;
            }    
        }

        public bool UpdateLink(LinksInfo link)
        {
            try
            {
                IDALinks.Update(link);
                IDALinks.UnitOfWork.Commit();
                return true;
            }
            catch (Exception)
            {
                return false;
            }    
        }

        public IList<LinksInfo> LoadAll(Expression<Func<LinksInfo, bool>> where, int pageIndex, int pageSize, params IOrderByExpression<LinksInfo>[] orderByExpressions)
        {
            return IDALinks.Filter(where, pageIndex, pageSize, orderByExpressions).ToList();
        }

        public int LoadCount(Expression<Func<LinksInfo, bool>> where)
        {
            return IDALinks.Count(where);
        }
    }
}
